package com.zm.study.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author
 * @ClassName: MasterDataSourceConfig
 * @Date: 2019/8/26
 * @Time: 9:55
 **/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionTemplate", sqlSessionTemplateRef = "masterSqlSessionTemplate")
@AutoConfigureAfter({MasterDataSourceConfig.class})
public class MasterDataSourceConfig {

    @Autowired
    private PageHelper pageHelper;

    protected static final String PACKAGE = "com.zm.study.master";

    protected static final String MAPPER_LOCATION = "classpath*:mybatis/*/*/*.xml";

    @Value("${spring.druid.sysadmin.datasource.url}")
    private String url;

    @Value("${spring.druid.sysadmin.datasource.username}")
    private String user;

    @Value("${spring.druid.sysadmin.datasource.password}")
    private String password;

    @Value("${spring.druid.sysadmin.datasource.driverClassName}")
    private String driverClass;

    @Value("${spring.druid.maxActive}")
    private int maxActive;

    @Value("${spring.druid.minIdle}")
    private int minIdel;

    @Value("${spring.druid.maxWait}")
    private long maxWait;

    @Bean(name = "sysAdminDataSource")
    public DataSource sysAdminDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(minIdel);
        dataSource.setMaxWait(maxWait);
        try {
            dataSource.setFilters("stat,wall,logback");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Properties properties = new Properties();
        properties.put("druid.stat.mergeSql","true");
        properties.put("druid.stat.slowSqlMillis","5000");
        dataSource.setConnectProperties(properties);
        dataSource.setUseGlobalDataSourceStat(true);
        return dataSource;
    }

    @Bean(name = "sysAdminTransactionManager")
    public DataSourceTransactionManager sysAdminTransactionManager(){
        return new DataSourceTransactionManager(sysAdminDataSource());
    }
    @Bean(name = "sysAdminSqlSessionFactory")
    public SqlSessionFactory sysAdminSessionFactory(@Qualifier("sysAdminDataSource") DataSource sysAdminDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        /**添加分页插件*/
        sessionFactory.setPlugins(new Interceptor[]{pageHelper});
        sessionFactory.setDataSource(sysAdminDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
        return sessionFactory.getObject();
    }
    @Bean(name = "masterSqlSessionTemplate")
    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("sysAdminSqlSessionFactory") SqlSessionFactory sysAdminSqlSessionFactory) {
        return new SqlSessionTemplate(sysAdminSqlSessionFactory);
    }

}
